package com.hb.umsp.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hb.umsp.entity.HbAttendance;
import com.hb.umsp.mapper.HbAttendanceMapper;
import com.hb.umsp.util.token.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AttendanceService {

    @Autowired
    HbAttendanceMapper hbAttendanceMapper;

    @Autowired
    TokenUtil tokenUtil;

    public void insert(List<HbAttendance> hbAttendanceList){

        for (int i=0; i<hbAttendanceList.size(); i++) {
            String attendanceDate = hbAttendanceList.get(i).getAttendanceDate();
            hbAttendanceList.get(i).setAttendanceMonth(
                    hbAttendanceList.get(i).getAttendanceDate().
                            replace("/","").
                            replace("-","").
                            substring(0,6));
        }

        hbAttendanceMapper.insert(hbAttendanceList);

    }

    public PageInfo<HbAttendance> findByPage(int pageNum, int pageSize,
                                             String token,HbAttendance hbAttendance) {
//        // 前台未传入考勤人员编码，从token中获取
//        if (hbAttendance != null && (hbAttendance.getAttendanceCode() == null ||
//                "".equals(hbAttendance.getAttendanceCode()))) {
//            Map<String,String> map = tokenUtil.parseToken(token);
//            String attendanceCode = map.get("attendanceCode");;
//            hbAttendance.setAttendanceCode(attendanceCode);
//        }

        PageHelper.startPage(pageNum, pageSize);
        /*String attendanceMonth = param.get("attendanceMonth");
        //如果传入的月份为空，默认查询当前月份的考勤
        if (attendanceMonth == null || "".equals(attendanceMonth)) {
            Date date = new Date();
            SimpleDateFormat dateTimeFormatter = new SimpleDateFormat("yyyy-MM-dd");
            attendanceMonth =  dateTimeFormatter.format(date).
                    replace("-","").substring(0,6);
            param.put("attendanceMonth",attendanceMonth);
        }*/
        List<HbAttendance> list = hbAttendanceMapper.selectHbAttendanceByUsername(hbAttendance);
        return new PageInfo<>(list,5);
    }

    public void deleteAttendance(HbAttendance hbAttendance) {
        hbAttendanceMapper.deleteAttendance(hbAttendance);
    }
}
